/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package centralehotelweb.converter;

import centralehotel.metier.backoffice.RessourcesMetierLocal;
import centralehotel.modele.Ville;
import centralehotelweb.RequestBean;
import centralehotelweb.SessionBean;
import centralehotelweb.bean.AbstractBean;
import java.util.Collection;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Mathieu
 */
public class VilleConverter extends AbstractBean implements  Converter {
    public Object getAsObject(FacesContext context, UIComponent component, String what) {
                Collection<Ville> liste= ((RequestBean)getBean("RequestBean")).getAllVille(); //lookupRessourcesMetier().getAllVille();
                for(Ville p : liste){
                    if(p.getNom().toLowerCase().equals(what.toLowerCase())){
                        System.out.println("Ville Trouvé---------------------"+what);
                        return p;
                    }
                }
                System.out.println("pas trouvé => nouvel ville -------------------");
                Ville v=new Ville();
                v.setNom(what);
		return v;
	}
 
	public String getAsString(FacesContext context, UIComponent component, Object what) {
            if(what != null && (what instanceof Ville))
                return ((Ville)what).getNom().toString();
            else
                throw new IllegalArgumentException("Objet de type Pays attendu");
               
	}  
   
}
